<?php

class user_relateModel extends Model {

	public function getFansListByUid($uid, $myUid, $timeStamp, $pageSize) {
		$list = $this->limit($pageSize)->order('add_time desc')->getAll('add_time<? and to_uid=?', [$timeStamp, $uid]);
		$resultData = [
			'list' => [],
			'lastTimeStamp' => $timeStamp,
		];
		if ($list) {
			$uids = [];
			foreach ($list as $row) {
				$uids[] = $row['uid'];
			}
			$uids = implode(',', $uids);
			//获取我的关注里是否有相同用户
			$myCollect = $this->query("select to_uid from wf_user_relate where uid={$myUid} and to_uid in ({$uids})")->fetchAll();
			$myCollectUids = [];
			if ($myCollect) {
				foreach ($myCollect as $row) {
					$myCollectUids[$row['to_uid']] = 1;
				}
			}
			//组装用户信息输出
			$userList = $this->query("select uid,nickname,avatar,dynamic_total,fans_total from wf_user where uid in ({$uids})")->fetchAll();
			if ($userList) {
				foreach ($userList as $row) {
					$resultData['list'][$row['uid']] = userModel::buildBaseUserInfo($row);
					$resultData['list'][$row['uid']]['dynamicTotal'] = intval($row['dynamic_total']);
					$resultData['list'][$row['uid']]['fansTotal'] = intval($row['fans_total']);
					$resultData['list'][$row['uid']]['isRelate'] = isset($myCollectUids[$row['uid']]) ? 1 : 0;

				}
				$resultData['list'] = array_values($resultData['list']);
			}
			$resultData['lastTimeStamp'] = $list[count($list) - 1]['add_time'];
		}
		return $resultData;
	}

	public function getFansList($uid, $timeStamp, $pageSize) {
		$list = $this->limit($pageSize)->order('add_time desc')->getAll('add_time<? and to_uid=?', [$timeStamp, $uid]);
		$resultData = [
			'list' => [],
			'lastTimeStamp' => $timeStamp,
		];
		if ($list) {
			$uids = [];
			foreach ($list as $row) {
				$uids[] = $row['uid'];
			}
			$uids = implode(',', $uids);
			//获取我的关注里是否有相同用户
			$myCollect = $this->query("select to_uid from wf_user_relate where uid={$uid} and to_uid in ({$uids})")->fetchAll();
			$myCollectUids = [];
			if ($myCollect) {
				foreach ($myCollect as $row) {
					$myCollectUids[$row['to_uid']] = 1;
				}
			}
			//组装用户信息输出
			$userList = $this->query("select uid,nickname,avatar,dynamic_total,fans_total from wf_user where uid in ({$uids})")->fetchAll();
			if ($userList) {
				foreach ($userList as $row) {
					$resultData['list'][$row['uid']] = userModel::buildBaseUserInfo($row);
					$resultData['list'][$row['uid']]['dynamicTotal'] = intval($row['dynamic_total']);
					$resultData['list'][$row['uid']]['fansTotal'] = intval($row['fans_total']);
					$resultData['list'][$row['uid']]['isRelate'] = isset($myCollectUids[$row['uid']]) ? 1 : 0;

				}
				$resultData['list'] = array_values($resultData['list']);
			}
			$resultData['lastTimeStamp'] = $list[count($list) - 1]['add_time'];
		}
		return $resultData;
	}

	public function getFollowListByUid($uid, $myUid, $timeStamp, $pageSize) {
		$list = $this->limit($pageSize)->order('add_time desc')->getAll('add_time<? and uid=?', [$timeStamp, $uid]);
		$resultData = [
			'list' => [],
			'lastTimeStamp' => $timeStamp,
		];
		if ($list) {
			$uids = [];
			foreach ($list as $row) {
				$uids[] = $row['to_uid'];
			}
			$uids = implode(',', $uids);
			//组装用户信息输出
			$myCollect = $this->query("select to_uid from wf_user_relate where uid={$myUid} and to_uid in ({$uids})")->fetchAll();
			$myCollectUids = [];
			if ($myCollect) {
				foreach ($myCollect as $row) {
					$myCollectUids[$row['to_uid']] = 1;
				}
			}

			$userList = $this->query("select uid,nickname,avatar,dynamic_total,fans_total from wf_user where uid in ({$uids})")->fetchAll();
			if ($userList) {
				foreach ($userList as $row) {
					$resultData['list'][$row['uid']] = userModel::buildBaseUserInfo($row);
					$resultData['list'][$row['uid']]['dynamicTotal'] = intval($row['dynamic_total']);
					$resultData['list'][$row['uid']]['fansTotal'] = intval($row['fans_total']);
					$resultData['list'][$row['uid']]['isRelate'] = isset($myCollectUids[$row['uid']]) ? 1 : 0;
				}
				$resultData['list'] = array_values($resultData['list']);
			}
			$resultData['lastTimeStamp'] = $list[count($list) - 1]['add_time'];
		}
		return $resultData;
	}

	public function getFollowListByUids($uid, $page, $pageSize) {
		$limit = ($page - 1) * $page_size;
		$sql = "SELECT r.to_uid FROM wf_user_relate r,wf_user u WHERE r.to_uid=u.uid and r.uid ={$uid} AND type > 2 limit {$limit}, {$pageSize}";
		$list = $this->query($sql)->fetchAll();
		$resultData = [
			'page' => $page,
			'pageSize' => $pageSize,
			'list' => [],
		];
		if ($list) {
			$uids = [];
			foreach ($list as $row) {
				$uids[] = $row['to_uid'];
			}
			$uids = implode(',', $uids);
			//组装用户信息输出
			$myCollect = $this->query("select to_uid,uid from wf_user_relate where uid in ({$uids}) AND to_uid =" . $uid)->fetchAll();
			$myCollectUids = [];
			if ($myCollect) {
				foreach ($myCollect as $row) {
					$myCollectUids[$row['uid']] = 1;
				}
			}

			$userList = $this->query("select uid,nickname,avatar,dynamic_total,fans_total from wf_user where uid in ({$uids})")->fetchAll();

			if ($userList) {
				foreach ($userList as $row) {
					$resultData['list'][$row['uid']] = userModel::buildBaseUserInfo($row);
					$resultData['list'][$row['uid']]['dynamicTotal'] = intval($row['dynamic_total']);
					$resultData['list'][$row['uid']]['fansTotal'] = intval($row['fans_total']);
					$resultData['list'][$row['uid']]['isRelate'] = isset($myCollectUids[$row['uid']]) ? 1 : 0;
				}
				$resultData['list'] = array_values($resultData['list']);
			}
		}
		return $resultData;
	}

	public function getFollowList($uid, $timeStamp, $pageSize) {
		$list = $this->limit($pageSize)->order('add_time desc')->getAll('add_time<? and uid=?', [$timeStamp, $uid]);
		$resultData = [
			'list' => [],
			'lastTimeStamp' => $timeStamp,
		];
		if ($list) {
			$uids = [];
			foreach ($list as $row) {
				$uids[] = $row['to_uid'];
			}
			$uids = implode(',', $uids);
			//组装用户信息输出
			$userList = $this->query("select uid,nickname,avatar,dynamic_total,fans_total from wf_user where uid in ({$uids})")->fetchAll();
			if ($userList) {
				foreach ($userList as $row) {
					$resultData['list'][$row['uid']] = userModel::buildBaseUserInfo($row);
					$resultData['list'][$row['uid']]['dynamicTotal'] = intval($row['dynamic_total']);
					$resultData['list'][$row['uid']]['fansTotal'] = intval($row['fans_total']);
					$resultData['list'][$row['uid']]['isRelate'] = 1;
				}
				$resultData['list'] = array_values($resultData['list']);
			}
			$resultData['lastTimeStamp'] = $list[count($list) - 1]['add_time'];
		}
		return $resultData;
	}
}